package isquirrel.servlets;

import isquirrel.beans.FriendLike;
import isquirrel.beans.Url;
import isquirrel.beans.User;
import isquirrel.error.CannotLoginUserException;
import isquirrel.recommender.NetworkRecommender;
import isquirrel.servlets.handler.HttpDBServlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.json.JSONException;

public class WhatYourFriendsLike extends HttpDBServlet {
	private static final long serialVersionUID = 1L;

    public WhatYourFriendsLike() {
    }


	protected void handleGet() throws ServletException, IOException,
			JSONException, CannotLoginUserException {
	}

	protected void handlePost() throws ServletException, IOException,
			JSONException, CannotLoginUserException {	
		String toPrint = "";
		//JSONObject jsonResponse = new JSONObject();
		User u = getLoggedInUser();
		List<FriendLike> popularUrls = NetworkRecommender.getFriendsUrls(u, 10);
		if (popularUrls == null || popularUrls.size() == 0) {
			String r = request.getRequestURI().split("/")[1];
			toPrint += "<p style='font-size:1em; color: #bbb'>It seems like your friends haven't liked anything yet. You could try widening your network of friends to get more results.<br><br><a style='font-weight:bold;font-size:1.4em;color:white;text-decoration:none; padding:3px; background-color:#A5C97F' href='/" +  r + "/servlet/Profiler?nextp=network&force=findfriends'>Find friends</a></p>";			
		}
		else {
			for (FriendLike like : popularUrls) {
				if (like != null) {
					Url url = like.url();
					toPrint +="<div class='friendlikecontainer' style='background-image: url(/iSquirrel/images/"+url.getThumb()+")'><a class='friendlike' target='_blank' href="+url.getAddress()+ ">"+url.getTitle() + "</a><div style='position:relative;top:14px;font-size:0.68em;color:#bbb'>Liked by " + like.likes() + " friends.</div><div style='width: 120px;display:block;font-size:0.60em;color:#ccc;position:absolute;right:0px;top:2px'>Ranking: <span class='friendlikeranking' style='width:" + like.ranking_css() + "px'></span><a class='ilikebtn' style='top:20px' onClick=\"like(0,this,'" + url.getAddress() + "','" + url.getTitle() + "',null,2)\"></a></div></div>";
				}
					
			}
		}
		response.getWriter().print(toPrint);
	}


}
